/////////////////////////////////////////////////////////////////////////////
// Defines interfaces for managing and monitoring winpcap adapters.
//
// Version 1.0 - 15 june 2007
//
// Copyright (c) 2007 by FlexByte Software (http://flexbyte.com)
// All rights reserved.
//
// You may use the source code in any product (freeware, commercial, etc.). 
// You may modify the source code as you want except 
// you cannot modify the copyright details at the top of each module.
// If you have a web site or blog give a link to our web site, flexbyte.com
/////////////////////////////////////////////////////////////////////////////
#ifndef AdapterManager_H
#define AdapterManager_H
#include <map>
#include <vector>
#include "FlexThread.h"
#include "FlexLock.h"
#include "Adapter.h"
/////////////////////////////////////////////////////////////////////////////
// CInterfaceEnumerator
/////////////////////////////////////////////////////////////////////////////
class CInterfaceEnumerator
	: public std::vector< pcap_if_t * >
{
public:
	CInterfaceEnumerator();
	~CInterfaceEnumerator();

private:
	pcap_if_t * m_devs;
};
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
struct IAdapterListener : public IPacketAnalyzer
{
	virtual void OpenAdapter(CAdapter & adapter, const char * name) = 0;
};
/////////////////////////////////////////////////////////////////////////////
// CAdapterManager
/////////////////////////////////////////////////////////////////////////////
class CAdapterManager
	: public IExecutable
{
public:
// Constructor/Destructor
	CAdapterManager(void);
	virtual ~CAdapterManager(void);

	static CAdapterManager & GetInstance();

// Attributes
	bool		IsLocalIp(DWORD dwHostAddr);

	int			GetCount() const;
	CAdapter *	GetAt(int iPos);
	CAdapter *	Get(DWORD dwHostAddr);

	bool		IsWorking() const;

// Implementation
	void		Capture(IAdapterListener * pListener);
	void		Release();
	void		Lock(); 
	void		Unlock();

protected:
	void		SetName();
	void		Execute();
	void		Break();

	CAdapter *	InsertAdapter(const char * szDeviceName);
	void		RefreshAdapters();

private:
	typedef std::vector< CAdapter * >		CAdapterArray;
	typedef std::map< DWORD, CAdapter * >	CIpAddressMap;

// Members
	CAdapterArray		m_arAdapters;
	CIpAddressMap		m_mapLocalAddress;
	IAdapterListener *	m_pListener;

	CFlexThread			m_threadMon;
	bool				m_bWork;
	HANDLE				m_hEvents[ 2 ];

	CFlexLock			m_lock;
};
/////////////////////////////////////////////////////////////////////////////
inline int CAdapterManager::GetCount() const
{
	return ( int )m_arAdapters.size();
}
/////////////////////////////////////////////////////////////////////////////
inline CAdapter * CAdapterManager::GetAt(int iPos)
{
	return m_arAdapters.at( iPos );
}
/////////////////////////////////////////////////////////////////////////////
inline bool CAdapterManager::IsWorking() const
{
	return m_bWork;
}
/////////////////////////////////////////////////////////////////////////////
inline void CAdapterManager::Lock()
{
	m_lock.Lock();
}
/////////////////////////////////////////////////////////////////////////////
inline void CAdapterManager::Unlock()
{
	m_lock.Unlock();
}
/////////////////////////////////////////////////////////////////////////////
#endif